package com.goktech.olala.client.controller.customer;

import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.service.ICustomerLoginService;
import com.goktech.olala.core.service.ICustomerService;
import com.goktech.olala.server.pojo.customer.Customer;
import com.goktech.olala.server.pojo.customer.CustomerLogin;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Classname RegController
 * @Description TODO
 * @Date 2020/12/10 12:30
 * @Created by ASUS
 */
@Controller
@RequestMapping("/cntApi")
public class RegController{

    @Autowired
    ICustomerService customerService;
    @Autowired
    ICustomerLoginService customerLoginService;

    @RequestMapping(value = "/register.do",method = RequestMethod.POST)
    @ResponseBody
    public String customerReg(HttpServletRequest request){

        ModelAndView mav = new ModelAndView();
        //ajax判断是否重复注册的参数
        String msg = "";

        //获取邮箱
        String email = request.getParameter("email");

        //判断邮箱是否已经注册过
        Customer customers = customerService.selectByEmail(email);
        if (customers!=null){//有注册信息
            msg="1";
            return msg;
        }else {
            msg="2";
            //密码
            String password = request.getParameter("password");
            //注册时间
            Date time = new Date();
            String regTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
            Date nowtime = Timestamp.valueOf(regTime);
            //id
            String customerId = String.valueOf(System.currentTimeMillis());
            //初始的用户注册名使用 用户+id
            String customerName = "用户"+customerId;

            //顾客类
            Customer customer = new Customer();
            customer.setEmail(email);
            //第一次注册时间与修改时间一致
            customer.setRegisterTime(nowtime);
            customer.setModifiedTime(nowtime);
            //id
            customer.setCustomerId(customerId);
            //第一次注册时的用户名
            customer.setCustomerName(customerName);
            //初始设置
            customer.setGender(Integer.parseInt(SysConfig.DEFAULT_GENDER));
            customer.setIdentyCardType(Integer.parseInt(SysConfig.DEFAULT_IDENTITY_CARD_TYPE));
            customer.setIdentyCardNo(SysConfig.DEFAULT_IDENTITY_CARD_NO);
            customer.setUserPoint(0);
            customer.setUserBalance(0);
            customer.setCustomerStatus(1);
            customer.setCustomerLevel(1);

            //添加用户信息到数据库
            int i = customerService.insert(customer);

            //用户登录类
            CustomerLogin customerLogin = new CustomerLogin();
            customerLogin.setCustomerId(customerId);
            customerLogin.setLoginName(email);
            customerLogin.setPassword(password);
            customerLogin.setUserStatus(0);
            customerLogin.setModifiedTime(nowtime);

            //添加用户的账号密码到数据库
            customerLoginService.insert(customerLogin);


        }
//        //跳转至登录界面
//        mav.setViewName("home/login");
        return msg;
    }


    //注册成功后跳转登录页面
    @RequestMapping(value = "/info.do",method = RequestMethod.GET)
    public ModelAndView fistRegister(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("home/login");
        return mav;
    }

    //用户登录
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    @ResponseBody
    public String customerLoginCheck(HttpServletRequest request){
        //获取用户名。密码
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        //ajax验证是否正确的中间值
        String msg="";
        //验证用户名密码的方法
        CustomerLogin customerLogin = customerLoginService.selectCustomer(userName, password);

        if (customerLogin==null){
            msg=SysConfig.ERROR_CODE_STR;
        }else {
            Customer customer = customerService.selectByEmail(userName);
            //直接把整个类传进去
            HttpSession session=request.getSession();//获取session
            session.setAttribute("LoginUser",customer);

            msg=SysConfig.SUCCESS_CODE_STR;
        }

        return msg;
    }

    //登录转到主页
    @RequestMapping("/index.do")
    @ResponseBody
    public ModelAndView toIndex(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("redirect:/index/init.do");
        return mav;
    }

    //退出登录
    @RequestMapping("/loginOut.do")
    @ResponseBody
    public String loginOut(HttpServletRequest request){
        String msg = "";
        HttpSession session = request.getSession();
        session.invalidate();//设置session失效
        msg="10000";
        return msg;
    }


}
